package com.bankLogisticsSys.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.bankLogisticsSys.model.Expressinfo;
import com.bankLogisticsSys.model.Expresspath;
import com.bankLogisticsSys.model.Networkinfo;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class NetWorkService {
	
	public void insert(Networkinfo network){
		network.save();
	}
	
	public Page<Networkinfo> getList(String name,String type,String sort, String order,int page,int limit){
		List<Object> para = new ArrayList<>();
		String showPath = "";
		if(type.equals("1")){//银行网点管理
			type = "(1,2)";
			showPath = ", if((select count(*) from expresspath where networkID = networkinfo.id)=0,'未设置','已设置')hasPath";
		}
		if(type.equals("2")){//邮政网点管理
			type = "(3,4)";
		}
		String condition = getCondition(name,sort, order, para);
		return Networkinfo.dao.paginate(page, limit, "select id,if(type=1,'银行网点',if(type=2,'银行分行',if(type=3,'邮政揽投部','分拨中心')))type, networkcode,network,if(state=0,'正常','停用')state "+ showPath, "from `networkinfo` where type in "+type+condition, para.toArray());
	}
	
	public Networkinfo getNetwork(int id){
		List<Networkinfo> list = Networkinfo.dao.find("select * from networkinfo where id = ?", id);
		if (list.size() == 0){
			return new Networkinfo();
		}
		else{
			System.out.println("what the fuck");
			return list.get(0);
		}

	}
	
	public List<Networkinfo> getSuperNetworkList(){
		List<Networkinfo> ni = Networkinfo.dao.find("select id, network from networkinfo where type in(2)");
		return ni;
	}
	
	public long getCount(String name,String type){
		if(type.equals("1")){//银行网点管理
			type = "(1,2)";
		}
		if(type.equals("2")){//邮政网点管理
			type = "(3,4)";
		}
		List<Object> para = new ArrayList<>();
		String conditoin = getCondition(name,"id", "asc", para);
		return Db.queryLong("select count(*) from networkinfo where type in "+type+conditoin,para.toArray());
	}
	
	public String getCondition(String name,String sort, String order,List<Object> para){
		String condition="";
		if(StringUtils.isNotBlank(name)){
			condition += " and network like ?";
			para.add("%"+name+"%");
		}
		condition += " order by " + sort + " " + order;
		return condition;
	}
	
	public boolean delete(Networkinfo network){
		if(Db.queryLong("select count(*) from expressrecord where networkid = ?", network.getId()) == 0){
			return network.delete();
		}
		return false;
	}
	
	public boolean edite(Networkinfo network){
		return network.update();
	}
	
	public boolean isExistNetworkCode(String networkCode){
		Networkinfo ni = Networkinfo.dao.findFirst("select id from networkinfo where networkcode = ?", networkCode);
		if(ni!=null){
			return true;
		}else{
			return false;
		}
	}
	
	public boolean isSource(int networkid){
		Networkinfo net = Networkinfo.dao.findById(networkid);
		return (net.getType()%2!=0);
	}
}
